package ro.ubbcluj.cs.bs;

import com.google.gson.Gson;
import ro.ubbcluj.cs.bs.common.dto.AuthorDto;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello World");

        try {

            System.out.println(" Getting ALL Authors!");
            getAll();
            System.out.println(" Get Ends! \n");

            System.out.println(" Posting new Author!");
            postGeza();
            System.out.println(" Posting Ends! \n");

            System.out.println(" Getting ALL Authors!");
            getAll();
            System.out.println(" Get Ends! \n");

            System.out.println(" Deleting Author!");
            deleteGeza(1);
            System.out.println(" Deleting Ends! \n");

            System.out.println(" Getting ALL Authors!");
            getAll();
            System.out.println(" Get Ends! \n");

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private static void deleteGeza(long id) throws IOException {
        URL url = new URL("http://localhost:8080/bsee-api/rest/author/" + id);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("DELETE");
        conn.setRequestProperty("Content-Type", "application/json");

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }
        conn.disconnect();
    }

    private static void postGeza() throws IOException {

        URL url = new URL("http://localhost:8080/bsee-api/rest/author/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        AuthorDto authorDto = new AuthorDto();
        authorDto.setFirstName("Geza");
        authorDto.setLastName("Ba");

        String input = new Gson().toJson(authorDto);


        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        conn.disconnect();
    }

    private static void getAll() throws IOException {

        URL url = new URL("http://localhost:8080/bsee-api/rest/author/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Content-Type", "application/json");

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();
    }
}
